<?php
namespace Home\Controller;
use Think\Controller;
class PublishController extends Controller {
	public $aid = 0; //文章id
	public $uid = 0; //用户id
	protected function _initialize(){
		$this->aid = I('aid',0,'intval');
		
		//记录
		if($this->aid){
			//记录访问数,记录访问来源
			flow_data($this->aid);
		}
		if(!empty($this->aid)){
			$info = M('Article')->where(array('id'=>$this->aid))->field('uid')->find();
			if(!empty($info)){
				//获取会员id(通过文章找到会员uid)
				$this->uid = $info['uid'];
				
				$level = M('User')->where(array('id'=>$info['uid']))->field('level')->find();
			
				if($level['level'] == '0'){
					$level_judge = 1; 
				}else{
					$level_judge = 0;
				}
			}
		}
		
		//上一链接(来源)
		$ref = $_SERVER['HTTP_REFERER'];
		if(strpos($ref,'Home/Article') !== false){
			$judge = true;
		}else{
			$judge = false;
		}
		
		//获取站点配置
		$this->opt = D('Options')->getOpts();
		
		//解序列化(公告)
	    $notice = unserialize($this->opt['notice']);

	    //分享参数
		$jsapi = wx_share();
		
		$this->assign(array(
			'source'=>C('resource'),
			'level_judge'=>$level_judge,
			'judge'=>$judge,
			'opt'=>$this->opt,
			'notice'=>$notice,
			'jsapi'=>$jsapi,
		));
		
		
	}  
	
	//显示文章内容
	public function article_detail(){
		 
		 //查询当前文章
		 $detail = D('Article')->article_detail($this->aid,1);
		 //组装url
		 $url    = "http://".$_SERVER['HTTP_HOST'].'/index.php/Home/Index/index';
		 
		 //组装分享url
		$share_url = "http://".$_SERVER['HTTP_HOST'].U('Home/Publish/article_detail',array('aid'=>$this->aid));
		 
		 if(empty($detail)){
			header('Location:'.U('Home/Index/index'));
		 }
		
		//拿取会员uid
		if(!empty($this->uid)){
			$uid = $this->uid;
			//是否开启二维码
			$qr = M("ArticleQr")->where("user_id = {$uid}")->field("close_qr")->find();

			//搜寻系统设置
			$setting_array = D('Customcode')->open_custom_setting();
			//获取当前类型
			$type          = $setting_array['type'];
			//如果当前会员选项是0的话，更新会员选项
			D('Customcode')->update_user_setting($uid,$type);
			//根据用户设置，返回对应url
			$return_array = D('Customcode')->return_different_code($uid);

			$code         = $return_array['code'];
			$code_img     = $return_array['code_img'];
		}
		
		 $detail['content'] = str_replace('position(this)','',$detail['content']);

		 //图片公共地址
		 $this->attachurl = C('IMAGE_HOST');

		 //加载变量
		 $this->assign(array(
			'detail'=>$detail,
			'url'=>$url,
			'share_url'=>$share_url,
			'qrcode_url'=>$code,
			'qr'=>$qr,
			'attachurl'=>$this->attachurl,
			 'code_img'=>$code_img
	     ));
		 
		$this->display('display_detail');
	}

	// 推广二维码
	public function qrcodeImg()
	{
		$user_model = D('User');

		$uid = I('uid',0,'intval');
		$uid = $uid > 0 ? $uid : session('home_uid');
		$user_info = $user_model->getMemberInfoById($uid);
		if (empty($user_info))
		{
			$this->error('错误操作',U('Home/Index/index'));
		}
		
		Vendor('phpqrcode.qrlib');
		$qrcode = new \QRcode();
		$qrcode::png($user_model->getMemberSpreadUrl($uid), false, 'L', 6 , 2);
		
	}
	
}